'use strict';

const Controller = require('egg').Controller;
const node_uid = require('node-uid')
console.log(node_uid(15), '----uit')

class HomeController extends Controller {

  async index() {
    this.ctx.body = {
      id: node_uid(15)
    }
  }

  async login() {
    const { ctx } = this;
    // 验证参数
    try {
      ctx.validate({ username: 'string', password: 'string' }, this.ctx.request.body);
    } catch (error) {
      ctx.status = 400;
      ctx.body = { msg: '参数传递有误', code: 0, error: error.errors[0] }
    }
    const data = await ctx.service.users.login()
    ctx.body = data;
  }

  async register() {
    const { ctx } = this;
    // 验证参数
    try {
      ctx.validate({ username: 'string', password: 'string', nickname: 'string' }, ctx.request.body);
    } catch (error) {
      ctx.status = 400;
      ctx.body = { code: 0, msg: '参数传递有误', error: error.errors[0] }
    }
    const data = await ctx.service.users.register()
    ctx.body = data;
  }

  async identity() {
    const { ctx } = this;
    if (ctx.state.user.userid === 0) {
      ctx.body = {
        code: 0,
        data: [
          {
            path: '/homeIndex/table',
            text: '表格'
          },
          {
            path: '/homeIndex/list',
            text: '列表路由'
          },
          {
            path: '/homeIndex/form',
            text: '表单路由'
          },
          {
            path: '/homeIndex/echarts',
            text: '图片路由'
          }
        ]
      }
    } else {
      ctx.body = {
        code: 0,
        data: [
          {
            path: '/homeIndex/table',
            text: '表格'
          },
          {
            path: '/homeIndex/echarts',
            text: '图片路由'
          }
        ]
      }
    }
  }
}

module.exports = HomeController;
